home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Graphics Samples / Bouncing Bitmap ƒ / Bouncing Bitmap.c next >
Encoding:
C/C++ Source or Header  |  1996-04-15  |  5.5 KB  |  182 lines  |  [TEXT/KAHL]

  1. /*
  2.     Bouncing bitmap 
  3.     
  4.     This application will get a pixmap from its resource file.  It will scale it to a smaller size.
  5.     The pixmap (i.e. gBitmapShape) will then bounce around the window. We check to make sure the 
  6.     bitmapShape will be drawn within the bounds of the window by checking the location of the 
  7.     bitmapShape against the windowBoundsShape before each GXDrawShape call. The bitmapShape will be 
  8.     rotated 12 degrees before being drawn by GXDrawShape.  
  9.  
  10.     NOTES:
  11.     • This file requires the following files to run correctly:
  12.         "graphics shell.c", "GraphicsDebugLibrary.c", "QDLibrary.c", "TransformLibrary.c".
  13.     
  14.     • For the best printing results, print this file in "landscape".
  15.  
  16.  
  17.     Change History:
  18.  
  19.        4/96    bob        Updated #includes to support changed GX Library names.
  20.                     Changed fixed to Fixed.
  21.                     Updated the note regarding the files needed to run, and copyright date.
  22.  
  23.     ©1990 - 1996 Apple Computer, Inc. 
  24.     All rights reserved.
  25. */
  26.  
  27.  
  28. #include <Events.h>
  29. #include <Windows.h>
  30.  
  31. #include "FontLibrary.h"
  32. #include <GXEnvironment.h>
  33. #include "GraphicsLibraries.h"
  34. #include <GXErrors.h>
  35. #include "QDLibrary.h"
  36. #include "graphics shell.h"
  37.  
  38. //
  39. //  Set up the title and size of the window 
  40. //
  41. Rect         gWindowQDRect = {40, 20, 340, 440};
  42. Str255         gWindowTitle = "\pBouncing bitmap";
  43.  
  44. //
  45. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  46. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  47. //    With  gGraphicsHeapSize set to 60k, I had 5 free blocks left in the graphics gxHeap. Sounds good to me.
  48. //
  49. long        gGraphicsHeapSize = 80;
  50.  
  51. gxShape     gBitmapShape;
  52. gxRectangle    gBitmapShapeBounds;
  53. Fixed         gDx, gDy, gDelta;
  54. gxRectangle gFixedWindowBounds;           //  bounding box of the window in local coordinates 
  55.  
  56.  
  57.  
  58. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  59.  
  60. void DoInitialization(theWindow)
  61. WindowPtr theWindow;
  62. {
  63.     gxShape clipShape;
  64.  
  65.     //
  66.     //    Get the bounds of the window in GX coordinates.  We will use this information in the 
  67.     //    DoIdle function to make sure "Clarus" does not bounce outside of the window.
  68.     //
  69.     GXGetShapeBounds(gWindowBoundsShape, 0L, &gFixedWindowBounds);
  70.  
  71.     //
  72.     //    Get the pixmap from the a resource file. If we fail in our attempt to create gBitmapShape, 
  73.     //    GXValidateShape will let us know via the debugger. 
  74.     //
  75.     gBitmapShape = GetPixMapShape(128);
  76.     GXValidateShape (gBitmapShape);
  77.  
  78.       GXScaleShape(gBitmapShape, fixed1/2, fixed1/2, 0, 0);
  79.  
  80.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  81.  
  82.     //
  83.     //  Setup the clip gxShape of the gBitmapShape
  84.     //
  85.     clipShape = GXNewRectangle(&gBitmapShapeBounds);
  86.     GXSetShapeClip(gBitmapShape, clipShape);
  87.     GXDisposeShape(clipShape);
  88.  
  89.     gDx = ff(5) + fixed1/2;
  90.     gDy = ff(6);
  91. }
  92.  
  93.  
  94.  
  95. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  96.  
  97. void DoIdle(theWindow)
  98. WindowPtr theWindow;
  99. {
  100.     gxMapping         map;
  101.     gxShape         boundsShape;
  102.  
  103.     GXRotateShape(gBitmapShape, ff(12), (gBitmapShapeBounds.left + gBitmapShapeBounds.right >> 1), (gBitmapShapeBounds.top + gBitmapShapeBounds.bottom >> 1));
  104.  
  105.     //
  106.     //  Get the bounds of the gBitmapShape and setup a gxRectangle that contains the bounds.
  107.     //
  108.     GXGetShapeBounds(gBitmapShape, 0L, &gBitmapShapeBounds);
  109.     boundsShape = GXNewRectangle (&gBitmapShapeBounds);
  110.     
  111.     //
  112.     //    Get the gxMapping of the gBitmapShape and set boundsShape mapping to it. We use boundsShape
  113.     //    to setup the "new" "bounds" of our bouncing gBitmapShape. "bounds" is then used below to make
  114.     //    sure that "Clarus" does not bounce outside of the window.
  115.     //
  116.     GXGetShapeMapping(gBitmapShape, &map);
  117.     GXMapShape(boundsShape, &map);
  118.     GXGetShapeBounds(boundsShape, 0L, &gBitmapShapeBounds);
  119.  
  120.     //
  121.     //    Make sure "Marilyn" doe not bounce outside of the window, by adjusting the "new" location
  122.     //    appropriately.
  123.     //
  124.     if ((gDelta = gFixedWindowBounds.left - gBitmapShapeBounds.left) > 0 || (gDelta = gFixedWindowBounds.right - gBitmapShapeBounds.right) < 0)
  125.      {    
  126.         GXMoveShape(gBitmapShape, gDx + gDelta, ff(0));    
  127.         gDx = -gDx;    
  128.      }
  129.  
  130.  
  131.     if ((gDelta = gFixedWindowBounds.top - gBitmapShapeBounds.top) > 0 || (gDelta = gFixedWindowBounds.bottom - gBitmapShapeBounds.bottom) < 0)
  132.      {    
  133.          GXMoveShape(gBitmapShape, ff(0), gDy + gDelta);    
  134.          gDy = -gDy;
  135.      }
  136.  
  137.     GXMoveShape(gBitmapShape, gDx, gDy);
  138.     GXDrawShape(gBitmapShape);
  139.  
  140.     GXDisposeShape(boundsShape);
  141. }
  142.  
  143.  
  144. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  145.  
  146. void DoDraw(theWindow)
  147. WindowPtr theWindow;
  148. {
  149.     GXDrawShape(gBitmapShape);
  150. }
  151.  
  152.  
  153.  
  154.  
  155. /*------ DoDispose ------------------------------------------------------------------------------------*/
  156.  
  157. void DoDispose(theWindow)
  158. WindowPtr theWindow;
  159. {
  160.     //  
  161.     //    You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  162.     //    form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  163.     //    call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  164.     //    SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  165.     //    can turn notices on in this file by setting gDebugging = TRUE (above).
  166.     //  
  167.     GXDisposeShape(gBitmapShape);
  168.     GXDisposeShape(gWindowBoundsShape);
  169.        DisposeWindow(theWindow);
  170. }
  171.  
  172.  
  173.  
  174. /*------ DoClick ---------------------------------------------------------------------------------------*/
  175.  
  176. void DoClick( orgMouseLoc, theWindow )
  177. gxPoint        orgMouseLoc;
  178. WindowPtr     theWindow;
  179. {
  180. }
  181.  
  182.